from flask import Blueprint, request, session
from flask_restful import Api, Resource, marshal_with
from flask_restful.reqparse import RequestParser

from webapp.config import api_utils
from webapp.dto_mapper import exam_page_fields, leave_page_fields
from webapp.schema import LeaveSchema
from webapp.service.leave_service import LeaveService

wx_leave_app = Blueprint('wxleave', __name__, url_prefix='/wxapi/wxleave')
wx_leave_api = Api(wx_leave_app)

parser = RequestParser()
parser.add_argument('id', location='json', type=int, trim=True, required=True)
parser.add_argument('uid', location='json', type=int, trim=True, required=False)
parser.add_argument('nickname', location='json', type=str, trim=True, required=False)
parser.add_argument('username', location='json', type=str, trim=True, required=False)
parser.add_argument('tname', location='json', type=str, trim=True, required=False)
parser.add_argument('start', location='json', type=str, trim=True, required=False)
parser.add_argument('reason', location='json', type=str, trim=True, required=False)
parser.add_argument('status', location='json', type=str, trim=True, required=False)

@wx_leave_api.resource("", "/")
class LeaveWxApi(Resource):
    def post(self):
        parser_post = parser.copy()
        parser_post.remove_argument('id')
        parser_post.parse_args()
        params = request.get_json()
        schema = LeaveSchema()
        curr_leaveinfo = schema.load(params, unknown="exclude")
        LeaveService.create(curr_leaveinfo=curr_leaveinfo)

@wx_leave_api.resource("", "/list")
class LeaveWxListApi(Resource):
    @marshal_with(leave_page_fields)
    def post(self):
        data = request.get_json()
        return LeaveService.query_leaveinfo_uid(data)